Page({
  data: {
    list: [],
    total: 0,
    // 分页参数
    query: {
      current: 1,
      pageSize: 5
    },
    isrefresher: false,
    isFinsh: false,
    isLoading: false,
    inError: false,
  },
  onShow() {
    this.initData()
  },
  // 获取访客邀请记录列表
  async initData() {
    this.setData({ isLoading: true })
    try {
      const { total, rows } = await wx.http.get('/visitor', this.data.query)
      // console.log(rows);
      this.setData({ list: [...this.data.list, ...rows], total })
      if (this.data.list.length < total) {
        this.setData({ 'query.current': this.data.query.current + 1 })
      } else {
        this.setData({ isFinsh: true })
      }
    } catch (err) {
      console.log(err);
      this.setData({ inError: true })
    } finally {
      this.setData({ isLoading: false })
    }

  },
  // 滚动加载
  scrolltolower() {
    if (this.data.isFinsh) return
    if (this.data.isLoading) return
    if (this.data.inError) return
    this.initData()
  },
  // 下拉刷新
  onRefresh() {
    this.setData({ list: [], 'query.current': 1, isFinsh: false, isLoading: false, inError: false })
    this.initData()
    wx.utils.toast('刷新成功')
    this.setData({ isrefresher: false })
  },
  // 出错点击
  onRush() {
    this.setData({ inError: false })
    this.initData()
  },
  // 点击跳转
  goPassport(ev) {
    wx.navigateTo({
      url: '/visitor_pkg/pages/passport/index?id=' + ev.mark.id,
    })
  },
})
